package com.xwj.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.xwj.dao.SysUserLogDao;
import com.xwj.entity.SysUserLog;
import com.xwj.service.Interface.SysUserLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by thinkjoy on 2017/7/25.
 */
@Service
public class SysUserLogServiceImpl implements SysUserLogService {

    @Autowired
    private SysUserLogDao sysUserLogDao;

    @Override
    public List<SysUserLog> findAllUserLogByAccountid(String AccountId) {
        return sysUserLogDao.findAllUserLogByAccountid(AccountId);
    }

    /**
     * 找出当前时间内7天的数据
     *
     * @param AccountId
     * @return
     */
    @Override
    public String findSevenDayLogByAccountid(String AccountId) {
        List<SysUserLog> sysUserLogs = sysUserLogDao.findSevenDayLogByAccountid(AccountId);
        String[] key = new String[]{"未来", "未来", "未来", "未来", "未来", "未来", "未来"};
        int[] value = new int[7];
        int i = 0;
        Map<String, Integer> map = new TreeMap<>();
        for (SysUserLog sysUserLog : sysUserLogs) {
            if (map.containsKey(sysUserLog.getLogindate())) {//判断是否已经有该数值，如有，则将次数加1
                map.put(sysUserLog.getLogindate(), map.get(sysUserLog.getLogindate()).intValue() + 1);
            } else {
                map.put(sysUserLog.getLogindate(), 1);
            }
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            key[i] = entry.getKey();
            value[i] = entry.getValue();
            i++;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", key);
        jsonObject.put("value", value);
        return jsonObject.toJSONString();
    }

    @Override
    public String addUserLoginLog(SysUserLog sysUserLog) {
        int row = sysUserLogDao.addUserLoginLog(sysUserLog);
        if (row > 0) {
            return "0";
        } else {
            return "1";
        }
    }
}
